package com.jltx.xq.simplewatch.video.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;

import com.jltx.xq.simplewatch.R;
import com.jltx.xq.simplewatch.base.BaseSupportFragment;
import com.jltx.xq.simplewatch.base.presenter.BasePresenter;
import com.jltx.xq.simplewatch.entity.beans.Channel;
import com.jltx.xq.simplewatch.video.adapter.VideoFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import me.weyye.library.colortrackview.ColorTrackTabLayout;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/19.
 */

public class VideoFragment extends BaseSupportFragment implements ViewPager.OnPageChangeListener{
    @BindView(R.id.tab_container_frg_video)
    FrameLayout frameLayout;

    @BindView(R.id.tab_fragment_frg_video)
    ColorTrackTabLayout tabLayout;

    @BindView(R.id.viewpager_frg_video)
    ViewPager viewPager;

    private VideoFragmentPagerAdapter videoFragmentPagerAdapter;
    private List<VideoFragmentPagerAdapter.VideoTabFragmentInfo> fragmentInfoList;

    public static VideoFragment newInstance() {
        VideoFragment fragment = new VideoFragment();
        return fragment;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_video_act;
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    public void initView(View rootView, Bundle savedInstanceState) {
        String[] channels = getResources().getStringArray(R.array.channel_video);
        String[] channelCodes = getResources().getStringArray(R.array.channel_code_video);
        fragmentInfoList = new ArrayList<>();
        for (int i=0; i < Math.min(channels.length,channelCodes.length); i++){
            String title = channels[i];
            String channel = channelCodes[i];
            fragmentInfoList.add(new VideoFragmentPagerAdapter.VideoTabFragmentInfo(new Channel(title,channel)));
        }

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        frameLayout.setVisibility(View.VISIBLE);
        videoFragmentPagerAdapter = new VideoFragmentPagerAdapter(getChildFragmentManager(),getActivity(),fragmentInfoList);
        viewPager.setAdapter(videoFragmentPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        //隐藏指示器
        tabLayout.setSelectedTabIndicatorHeight(0);
        viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //当页签切换的时候，如果有播放视频，则释放资源
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
